/**
 * @author 雷明浩
 * @date 2025/6/3 16:42
 */
public class FindPeakElement {
    // newcoder 寻找峰值
    // https://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
    public int findPeakElement (int[] nums) {
        // write code here
        int len = nums.length;
        int left = 0, right = len-1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if ((mid-1 < 0 && mid + 1 >= len)
                    || (mid-1 < 0 && nums[mid] > nums[mid+1])
                    || (mid+1 >= len && nums[mid] > nums[mid-1])
                    || (mid-1 >= 0 && mid +1 < len && nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1])) {
                return mid;
            }else if (nums[mid] < nums[mid+1]) {
                left = mid + 1;
            }else if (nums[mid] > nums[mid+1]) {
                right = mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{2};
        FindPeakElement peakElement = new FindPeakElement();
        System.out.println(peakElement.findPeakElement(nums));
    }
}